﻿/*
    调用方式：
    var cl = new WJH_Category_Plus();
    cl.Category1ID="select1";
    cl.Category2ID="select2";
    cl.Category3ID="select3";
    cl.DataURL="/Public/GetProductCategorys";
    cl.Init("DIV1",0,0,0,true,function(){});
*/
function WJH_Category_Plus() {
    this.Category1ID = "";
    this.Category2ID = "";
    this.Category3ID = "";
    this.DataURL = "";
    /*
        wrapID:包裹三级分类的标签ID
        category1:省ID对应value
        category2:市ID对应value
        category3:县ID对应 value
        userEmpty:是否支持请选择，如果为false则默认三级都加载
        successCallBack:加载完成后的回调函数
    */
    this.Init = function (wrapID, category1, category2, category3, useEmpty, successCallBack) {
        this.InitTag(wrapID, useEmpty);
        this.InitData(category1, category2, category3, useEmpty, successCallBack);
        this.category1Select(useEmpty);
        this.category2Select(useEmpty);
    };
    this.InitTag = function (wrapID, useEmpty) {
        var tmpInit = "";
        tmpInit += "<span class='wjh_category1_span'>一级分类：</span>";
        if (useEmpty) {
            tmpInit += "<select id='" + this.Category1ID + "' name='" + this.Category1ID + "'><option value='0'>--请选择--</option></select>";
        }
        else {
            tmpInit += "<select id='" + this.Category1ID + "' name='" + this.Category1ID + "'></select>";
        }
        tmpInit += "<span class='wjh_category2_span'>二级分类：</span>";
        if (useEmpty) {
            tmpInit += "<select id='" + this.Category2ID + "' name='" + this.Category2ID + "'><option value='0'>--请选择--</option></select>";
        }
        else {
            tmpInit += "<select id='" + this.Category2ID + "' name='" + this.Category2ID + "'></select>";
        }
        tmpInit += "<span class='wjh_category3_span'>三级分类：</span>";
        if (useEmpty) {
            tmpInit += "<select id='" + this.Category3ID + "' name='" + this.Category3ID + "'><option value='0'>--请选择--</option></select>";
        }
        else {
            tmpInit += "<select id='" + this.Category3ID + "' name='" + this.Category3ID + "'></select>";
        }
        $("#" + wrapID).html(tmpInit);
    };
    this.InitData = function (category1, category2, category3, userEmpty, successCallBack) {
        var c1 = this.Category1ID;
        var c2 = this.Category2ID;
        var c3 = this.Category3ID;
        var dataUrl = this.DataURL;
        if (category1 == 0) {
            $.get(dataurl, {}, function (category1) {
                var firstcategory1Guid = category1[0].Value;
                //初始化一级分类
                for (var i = 0, len = category1.length; i < len; i++) {
                    var tmp_option = "<option value='" + category1[i].Value + "'>" + category1[i].Display + "</option>";
                    $("#" + c1).html($("#" + c1).html() + tmp_option);
                }

                if (userEmpty) {
                    successCallBack && successCallBack();
                    return;
                }
                //初始化二级分类
                $.get(dataUrl, { pid: firstcategory1Guid }, function (category2) {
                    var secondcategory2Guid = category2[0].Value;
                    for (var i = 0, len = category2.length; i < len; i++) {
                        var tmp_option = "<option value='" + category2[i].Value + "'>" + category2[i].Display + "</option>";
                        $("#" + c2).html($("#" + c2).html() + tmp_option);
                    }
                    //初始化县
                    $.get(dataUrl, { pid: secondcategory2Guid }, function (category3) {
                        for (var i = 0, len = category3.length; i < len; i++) {
                            var tmp_option = "<option value='" + category3[i].Value + "'>" + category3[i].Display + "</option>";
                            $("#" + c3).html($("#" + c3).html() + tmp_option);
                        }
                        successCallBack && successCallBack();
                    }, "json");
                }, "json");
            }, "json");
        }
        else {
            $.get(dataUrl, {}, function (cate1) {
                //初始化一级分类
                for (var i = 0, len = cate1.length; i < len; i++) {
                    var tmp_option = "";
                    if (cate1[i].Value == category1) {
                        tmp_option = "<option selected='selected' value='" + cate1[i].Value + "'>" + cate1[i].Display + "</option>";
                    }
                    else {
                        tmp_option = "<option value='" + cate1[i].Value + "'>" + cate1[i].Display + "</option>";
                    }
                    $("#" + c1).html($("#" + c1).html() + tmp_option);
                }
                //初始化二级分类
                $.get(dataUrl, { pid: category1 }, function (cate2) {
                    for (var i = 0, len = cate2.length; i < len; i++) {
                        if (cate2[i].Value == category2) {
                            tmp_option = "<option selected='selected' value='" + cate2[i].Value + "'>" + cate2[i].Display + "</option>";
                        }
                        else {
                            tmp_option = "<option value='" + cate2[i].Value + "'>" + cate2[i].Display + "</option>";
                        }
                        $("#" + c2).html($("#" + c2).html() + tmp_option);
                    }
                    //初始化三级分类
                    $.get(dataUrl, { pid: category2 }, function (cate3) {
                        for (var i = 0, len = cate3.length; i < len; i++) {
                            var tmp_option = "";
                            if (cate3[i].Value == category3) {
                                tmp_option = "<option selected='selected' value='" + cate3[i].Value + "'>" + cate3[i].Display + "</option>";
                            }
                            else {
                                tmp_option = "<option value='" + cate3[i].Value + "'>" + cate3[i].Display + "</option>";
                            }
                            $("#" + c3).html($("#" + c3).html() + tmp_option);
                        }
                    }, "json");
                }, "json");
            }, "json");
        }
    };
    //一级分类change
    this.category1Select = function (userEmpty) {
        var c1 = this.Category1ID;
        var c2 = this.Category2ID;
        var c3 = this.Category3ID;
        var dataUrl = this.DataURL;
        $("#" + c1).change(function () {
            var optionHtml = "";
            if (userEmpty) {
                optionHtml = "<option value='0'>--请选择--</option>";
            }
            $("#" + c2).html(optionHtml);
            $("#" + c3).html(optionHtml);
            var firstcategory1 = $("#" + c1 + " option:selected").val();
            //初始化二级分类
            $.get(dataUrl, { pid: firstcategory1 }, function (category2) {
                var secondcategory2 = category2[0].Value;
                for (var i = 0, len = category2.length; i < len; i++) {
                    var tmp_option = "<option value='" + category2[i].Value + "'>" + category2[i].Display + "</option>";
                    $("#" + c2 + "").html($("#" + c2 + "").html() + tmp_option);
                }
                if (userEmpty) return;
                //初始化三级分类
                $.get(dataUrl, { pid: secondcategory2 }, function (category3) {
                    for (var i = 0, len = category3.length; i < len; i++) {
                        var tmp_option = "<option value='" + category3[i].Value + "'>" + category3[i].Display + "</option>";
                        $("#" + c3).html($("#" + c3).html() + tmp_option);
                    }
                }, "json");
            }, "json");
        });
    };
    //二级分类change
    this.category2Select = function (userEmpty) {
        var c1 = this.Category1ID;
        var c2 = this.Category2ID;
        var c3 = this.Category3ID;
        var dataUrl = this.DataURL;
        $("#" + c2).change(function () {
            var optionHtml = "";
            if (userEmpty) {
                optionHtml = "<option value='0'>--请选择--</option>";
            }
            $("#" + c3).html(optionHtml);
            var tmpSelectedCategory2 = $("#" + c2 + " option:selected").val();
            //初始化三级分类
            $.get(dataUrl, { pid: tmpSelectedCategory2 }, function (category3) {
                for (var i = 0, len = category3.length; i < len; i++) {
                    var tmp_option = "<option value='"+category3[i].Value+"'>"+category3[i].Display+"</option>";
                    $("#" + c3).html($("#" + c3).html() + tmp_option);
                }
            }, "json");
        });
    };
}

(function ($) {

})(jQuery);